package app.ui;

import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import app.action.WorkPanelAction;

public class WorkPanel extends AbsCommonPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8889516548428633127L;

	JTextField fldDir; // directory
	JTextField fldLtn; // list name
	JTextArea fldLog; // output field
	JScrollPane scrFldLog; // hold fldLog
	JLabel lblSta; // state

	JButton btnDir; // select directory
	JButton btnLtn; // select list name
	JButton btnCmp; // compare command

	public WorkPanel(AbsMainFrame mFrame) {
		super(mFrame);
	}

	@Override
	protected void addComponents() {
		fldDir = new JTextField();
		add(fldDir);
		fldLtn = new JTextField();
		add(fldLtn);

		fldLog = new JTextArea();
		scrFldLog = new JScrollPane(fldLog);
		add(scrFldLog);

		lblSta = new JLabel("State: none");
		add(lblSta);

		btnDir = new JButton("Directory");
		add(btnDir);
		btnLtn = new JButton("List Name");
		add(btnLtn);
		btnCmp = new JButton("Compare");
		add(btnCmp);
	}

	@Override
	protected void initializeComponents() {
		fldDir.setEditable(false);
		fldLtn.setEditable(false);

		//
		WorkPanelAction action = new WorkPanelAction(this);
		btnDir.addActionListener(action);
		btnCmp.addActionListener(action);
		btnLtn.addActionListener(action);
	}

	@Override
	protected void resizeComponents() {
		final int dis = 5;
		int x = 2 * dis;
		int y = x;
		int width = getWidth() - btnWid - 2 * x;
		int height = btnHei;

		// fldDir
		fldDir.setBounds(x, y, width, height);

		// fldLtn
		y += height + dis;
		fldLtn.setBounds(x, y, width, height);

		// lblSta
		y += height + dis;
		lblSta.setBounds(x, y, width, height);

		// btnDir
		y = x;
		x += width + dis;
		width = btnWid;
		btnDir.setBounds(x, y, width, height);

		// btnLtn
		y += height + dis;
		btnLtn.setBounds(x, y, width, height);

		// btnCmp
		y += height + dis;
		btnCmp.setBounds(x, y, width, height);

		// scrFldLog
		x = 2 * dis;
		y += height + dis;
		width = getWidth() - 2 * x;
		height = getHeight() - y - dis;
		scrFldLog.setBounds(x, y, width, height);
	}

	public void diplayData(int cout, LinkedList<String> missingFiles) {
		fldLog.setText("");
		for(String str : missingFiles)
			fldLog.setText(fldLog.getText() + str + '\n');

		StringBuilder sb = new StringBuilder("State: ");
		sb.append("Pass " + cout + "  Fail " + String.valueOf(missingFiles.size()));
		lblSta.setText(sb.toString());
	}

	public void setTextFldDir(String text) {
		fldDir.setText(text);
	}

	public void setTextFldLtn(String text) {
		fldLtn.setText(text);
	}

	public void setTextlblSta(String text) {
		lblSta.setText(text);
	}
}
